package com.maddox.maddoxppjoke.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;

import com.alibaba.fastjson.JSONObject;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.maddox.libnetwork.ApiResponse;
import com.maddox.libnetwork.GetRequest;
import com.maddox.libnetwork.JsonCallback;
import com.maddox.maddoxppjoke.R;
import com.maddox.maddoxppjoke.util.NavGraphBuilder;

public class HomeActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private NavController navController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        //获取导航视图控制器
        navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        //设置控制器
        NavGraphBuilder.build(navController, this, R.id.nav_host_fragment);
        //初始化底部菜单
        BottomNavigationView navView = findViewById(R.id.bottom_bar);
        //对导航的页面和底部菜单进行关联
       navView.setOnNavigationItemSelectedListener(this);

       //测试网络
//        GetRequest<String> getRequest  = new GetRequest<>("www.baidu.com");
//        getRequest.execute(new JsonCallback<String>() {
//            @Override
//            public void onSuccess(ApiResponse<String> response) {
//                Log.d("Maddox",response.dat);
//            }
//
//        });

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        //根据选中的菜单id进行跳转
        navController.navigate(item.getItemId());
        //返回true说明被选中,会对图标文本进行着色,考虑到中间的无标题大菜单,通过是否有标题来进行确认是否着色
        return !TextUtils.isEmpty(item.getTitle());
    }
}